var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var session = require("express-session")

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var menuRouter = require('./routes/menu');
var cityRouter = require('./routes/city');

var app = express();
// session 配置
app.use(session({
    secret:"dfjdlfajdfafdafadfadfaf",
    cookie:{ maxAge:1000*60*60*24},
    resave:true,
    saveUninitialized:true
}))
/* 后台开允许跨域 */
app.all('*', (req, res, next) => {
    const { origin, Origin, referer, Referer } = req.headers;
    const allowOrigin = origin || Origin || referer || Referer || '*';
      res.header("Access-Control-Allow-Origin", "*");
      res.header("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With");
      res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("Access-Control-Allow-Credentials", true); //可以带cookies
      res.header("X-Powered-By", 'Express');
      if (req.method == 'OPTIONS') {
        res.sendStatus(200);
      } else {
      next();
      }
  });
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter); // login method post
app.use('/users', usersRouter);
app.use("/menu",menuRouter);
app.use("/city",cityRouter)

module.exports = app;
